package com.yangluyao.easyexcel.utils;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: shengji
 * @description:
 * @author: LL
 * @create: 2020-05-13 14:52
 **/
@ApiModel("返回值实体")
@Data
@JsonSerialize()
public class ApiResponse<T> implements Serializable {

    private @ApiModelProperty("自定义返回的状态")
    String status;
    private @ApiModelProperty("自定义状态的消息内容")
    String message;
    /**
     * 数据
     */
    private @ApiModelProperty("数据")
    T data;


    /**
     * 签名
     */
    private @ApiModelProperty("签名")
    String sign;


    public static <T> ApiResponse success(T data) {
        return response(SuccessStatus.OPERATION_SUCCESS.status(), SuccessStatus.OPERATION_SUCCESS.message(), data, false);
    }

    public static ApiResponse success(String msg) {
        return response(SuccessStatus.OPERATION_SUCCESS.status(), msg, null, false);
    }

    public static <T> ApiResponse success(T data, boolean signFlag) {
        return response(SuccessStatus.OPERATION_SUCCESS.status(), SuccessStatus.OPERATION_SUCCESS.message(), data, false);
    }

    public static <T> ApiResponse success(T data, String msg) {
        return response(SuccessStatus.OPERATION_SUCCESS.status(), msg, data, false);
    }


    public static ApiResponse error(String code, String msg) {
        return response(code, msg, null);
    }

    public static ApiResponse error(ErrorStatus status) {
        return response(status.status(), status.message(), null);
    }

    public static ApiResponse error(HttpStatus status, ErrorStatus errorStatus) {
        return response(status.value() + "", status.getReasonPhrase(), errorStatus.message());
    }

    public static <T> ApiResponse response(String status, String msg, T data) {

        return response(status, msg, data, false);
    }


    public static <T> ApiResponse response(String status, String msg, T data, boolean signFlag) {
        ApiResponse response = new ApiResponse();
        response.setData(data);
        response.setStatus(status);
        response.setMessage(msg);
        return response;
    }


    /**
     * @param data 反射的对象,获取对象的字段名和值
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static Map<String, String> getFields(Object data) throws IllegalAccessException, IllegalArgumentException {
        if (data == null) {
            return null;
        }
        Map<String, String> map = new HashMap<>();
        Field[] fields = data.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            field.setAccessible(true);


            String name = field.getName();
            Object value = field.get(data);
            if (field.get(data) != null) {
                map.put(name, value.toString());
            }
        }


        return map;
    }
}
